﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestPlanWordWriter.UI.Properties;
using TestPlanWordWriter.Common.Word;

namespace TestPlanWordWriter.UI
{
    public class TestPlanDocWriterSettingsManager
    {
        private TestPlanDocWriterSettings _docWriterSettings = new TestPlanDocWriterSettings();

        public TestPlanDocWriterSettings DocWriterSettings
        {
            get { return _docWriterSettings; }
        }

        public void Load()
        {
            _docWriterSettings.TestPlanStyleName = Settings.Default.TestPlanStyleName;
            _docWriterSettings.TestPlanDescriptionStyleName = Settings.Default.TestCaseDescriptionStyleName;

            _docWriterSettings.TestCaseStyleName = Settings.Default.TestCaseStyleName;
            _docWriterSettings.TestStepsTableStyleName = Settings.Default.TestStepsTableStyleName;

            _docWriterSettings.TestSuiteLevelsSettings = DeserializeTestSuiteLevelsSettings(Settings.Default.TestSuiteLevelsSettings);

            _docWriterSettings.TestCaseDescriptionStyleName = Settings.Default.TestCaseDescriptionStyleName;

            _docWriterSettings.TableLabel_Step = Settings.Default.TableLabel_Step;
            _docWriterSettings.TableLabel_Action = Settings.Default.TableLabel_Action;
            _docWriterSettings.TableLabel_ExpectedResult = Settings.Default.TableLabel_ExpectedResult;
        }

        public void Save()
        {
            Settings.Default.TestPlanStyleName = _docWriterSettings.TestPlanStyleName;
            Settings.Default.TestPlanDescriptionStyleName = _docWriterSettings.TestPlanDescriptionStyleName;

            Settings.Default.TestCaseStyleName = _docWriterSettings.TestCaseStyleName;
            Settings.Default.TestStepsTableStyleName = _docWriterSettings.TestStepsTableStyleName;
            Settings.Default.TestCaseDescriptionStyleName = _docWriterSettings.TestCaseDescriptionStyleName;

            Settings.Default.TestSuiteLevelsSettings = SerializeTestSuiteLevelsSettings(_docWriterSettings.TestSuiteLevelsSettings);

            Settings.Default.TableLabel_Step = _docWriterSettings.TableLabel_Step;
            Settings.Default.TableLabel_Action = _docWriterSettings.TableLabel_Action;
            Settings.Default.TableLabel_ExpectedResult = _docWriterSettings.TableLabel_ExpectedResult;

            Settings.Default.Save();
        }

        private string SerializeTestSuiteLevelsSettings(List<TestSuiteSettings> suiteLevelsSettings)
        {
            StringBuilder builder = new StringBuilder();
            int settingsCount = suiteLevelsSettings.Count;
            for (int i = 0; i < settingsCount; i++)
            {
                TestSuiteSettings suiteSettings = suiteLevelsSettings[i];
                builder.Append(suiteSettings.TestSuiteStyleName);
                builder.Append(",");
                builder.Append(suiteSettings.TestSuiteDescription);
                builder.Append(";");
            }
            return builder.ToString();
        }

        private List<TestSuiteSettings> DeserializeTestSuiteLevelsSettings(string settingValue)
        {
            List<TestSuiteSettings> suiteLevelsSettings = new List<TestSuiteSettings>();

            //TestSuiteStyles are in the form: "suitetitlestyle1,suitedescriptionStyle1;suitetitleStyle2,suiteDescriptionStyle2;...
            string[] levelsStyles = settingValue.Split(';');
            foreach (string levelStyle in levelsStyles)
            {
                if (!string.IsNullOrEmpty(levelStyle))
                {
                    string[] styles = levelStyle.Split(',');
                    TestSuiteSettings suiteSettings = new TestSuiteSettings();
                    suiteSettings.TestSuiteStyleName = styles[0].Trim();
                    suiteSettings.TestSuiteDescription = styles[1].Trim();
                    suiteLevelsSettings.Add(suiteSettings);
                }
            }

            return suiteLevelsSettings;
        }

    }
}
